package com.youmans.minio.controller;

import com.youmans.common_utils.config.Result;
import com.youmans.minio.config.MinioProp;
import com.youmans.minio.utils.FileUtils;
import com.youmans.minio.utils.MinioUtils;
import com.youmans.minio.utils.search.SQuery;
import io.minio.MinioClient;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.entity.ContentType;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;

//@CrossOrigin
@Controller
@RequestMapping("/admin/minio/file")
public class MinioController {
 
    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private MinioProp minioProp;

    @ResponseBody
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file)throws IOException {
        try {
            String uploadFile = minioUtils.uploadFile(file, minioProp.getBucketname());
            return Result.ok(uploadFile).message("文件上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail().message("文件上传失败");
        }
    }
    @ResponseBody
    @PostMapping("/upload_two")//请求中获取数据
    public Result upload_two(HttpServletRequest request)throws IOException {
        try {
            MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest) request;
            MultipartFile file = multipartRequest.getFile("file");
            String uploadFile = minioUtils.uploadFile(file, minioProp.getBucketname());
            return Result.ok(uploadFile).message("文件上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail().message("文件上传失败");
        }
    }

    @ResponseBody
    @PostMapping("/uploadByUrl")//根据单个路径上传
    public Result uploadByUrl(String fileName)throws IOException {
        try {
            File file = new File(fileName);
//            File file = new File("/Library/d7ca8dd1c3bebdf26a2c8f103cfbd901.jpg");
            FileInputStream inputStream = new FileInputStream(file);
            MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                    ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
            String uploadFile = minioUtils.uploadFile(multipartFile, minioProp.getBucketname());
            return Result.ok(uploadFile).message("文件上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail().message("文件上传失败");
        }
    }
    @ResponseBody
    @PostMapping("/uploadByUrls")//根据路径批量上传
    public Result uploadByUrls(@RequestBody(required = true) SQuery sQuery)throws Exception{
//        try {
        String fileName = sQuery.getFileName() ;
            File file = new File(fileName);
        ArrayList<String> newFileNames = new ArrayList<>() ;
        ArrayList<String> oriFileNames = new ArrayList<>() ;
        Map map = new HashMap();
            if(!file.exists()){
                return Result.fail().message("文件夹路径错误");
            }else{
                if(file.isFile()){
                    if(FileUtils.isImageBySuffix(fileName.substring(fileName.lastIndexOf(".") + 1))&&FileUtils.isImage(new FileInputStream(fileName))) {//判断是否为图片
                        FileInputStream inputStream = new FileInputStream(file);
                        MultipartFile multipartFile = new MockMultipartFile(new File(fileName).getName(), new File(fileName).getName(),
                                ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
                        String uploadFile = minioUtils.uploadFile(multipartFile, minioProp.getBucketname());
                        oriFileNames.add(fileName.substring(fileName.lastIndexOf("\\") + 1));
                        newFileNames.add(uploadFile);
//                        map.put("oriFileNames",oriFileNames);
//                        map.put("newFileNames",newFileNames);
                    }else{
                        return Result.fail().message("图片格式错误");
                    }
                }else {

                    ArrayList<String> listDir = FileUtils.listDir(file);
                    for (String file1 : listDir) {
                        try {
                            if(FileUtils.isImageBySuffix(file1.substring(file1.lastIndexOf(".") + 1))&&FileUtils.isImage(new FileInputStream(file1))) {
                                FileInputStream inputStream = new FileInputStream(file1);
                                MultipartFile multipartFile = new MockMultipartFile(new File(file1).getName(), new File(file1).getName(),
                                        ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
                                String uploadFile = minioUtils.uploadFile(multipartFile, minioProp.getBucketname());
                                oriFileNames.add(file1.substring(file1.lastIndexOf("\\") + 1));
                                newFileNames.add(uploadFile);
//                                map.put("oriFileNames", oriFileNames);
//                                map.put("newFileNames", newFileNames);
                            }
                        } catch (IOException e) {
                            return Result.fail("上传故障");
                        }
                    }
                }
            }
//            File file = new File("/Library/d7ca8dd1c3bebdf26a2c8f103cfbd901.jpg");
//            FileInputStream inputStream = new FileInputStream(file);
//            MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
//                    ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
//            String uploadFile = minioUtils.uploadFile(multipartFile, minioProp.getBucketname());

        map.put("oriFileNames", oriFileNames);
        map.put("newFileNames", newFileNames);
            return Result.ok(map).message("文件上传成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return Result.fail().message("文件上传失败");
//        }
    }


    //文件删除
    @DeleteMapping("/remove")
    @PostMapping
    public Result delete(@RequestParam("name") String name) {
        try {
            MinioClient minioClient = new MinioClient(minioProp.getEndpoint(), minioProp.getAccesskey(), minioProp.getSecretKey());
            minioClient.removeObject(minioProp.getBucketname(),name);
        } catch (Exception e) {
            return Result.fail().message("删除失败");
        }
        return Result.ok().message("删除成功");
    }
}
 